package com.eastfair.venueservice.service.impl;

import com.eastfair.builder.BuilderRegisterFeign;
import com.eastfair.builder.dto.BindBuilderServiceProviderRoleDTO;
import com.eastfair.builder.dto.RemoveBuilderServiceProviderRoleDTO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.entity.ServiceProvider;
import com.eastfair.venueservice.enumeration.ServiceProviderRoleEnum;
import com.eastfair.venueservice.service.VenueManageBuilderRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 搭建商注册服务.
 *
 * @author dqq
 * @date 2022 /8/15
 */
@Service
@Slf4j
public class VenueManageBuilderRegisterServiceImpl implements VenueManageBuilderRegisterService {

    @Resource
    private BuilderRegisterFeign builderRegisterFeign;

    @Override
    public boolean bindBuilderServiceProviderRole(BindBuilderServiceProviderRoleDTO bindBuilderServiceProviderRoleDTO) {
        R<Boolean> result = builderRegisterFeign.bindHomeServiceProviderRole(bindBuilderServiceProviderRoleDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean bindBuilderServiceProviderRole(Long userId, Long serviceProviderId, String serviceProviderName, String serviceProviderRole) {
        BindBuilderServiceProviderRoleDTO roleDTO = new BindBuilderServiceProviderRoleDTO();
        roleDTO.setServiceProviderRole(serviceProviderRole);
        roleDTO.setUserId(userId);
        roleDTO.setServiceProviderId(serviceProviderId);
        roleDTO.setServiceProviderName(serviceProviderName);
        return bindBuilderServiceProviderRole(roleDTO);
    }

    @Override
    public boolean removeBuilderServiceProviderRole(List<RemoveBuilderServiceProviderRoleDTO> removeBuilderServiceProviderRoleDTOList) {
        R<Boolean> result = builderRegisterFeign.removeHomeServiceProviderRole(removeBuilderServiceProviderRoleDTOList);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean removeRole(List<ServiceProvider> list, ServiceProviderRoleEnum serviceProviderRoleEnum) {
        List<RemoveBuilderServiceProviderRoleDTO> roleList = list
                .stream()
                .map(serviceProvider -> RemoveBuilderServiceProviderRoleDTO
                        .builder()
                        .serviceProviderId(serviceProvider.getId())
                        .userId(serviceProvider.getUserId())
                        .serviceProviderRole(serviceProviderRoleEnum.getCode())
                        .build())
                .collect(Collectors.toList());
        return removeBuilderServiceProviderRole(roleList);
    }
}